package com.helloae.chap06;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory;
import com.esri.arcgis.geoanalyst.FeatureClassDescriptor;
import com.esri.arcgis.geoanalyst.RasterConversionOp;
import com.esri.arcgis.geoanalyst.esriRasterEnvSettingEnum;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IWorkspace;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 将shp文件转换为GRID的栅格文件，将shp的一个字段赋值给栅格，如果值相同，将这二者合并<br>
 * 示例章节：6.5.2
 * @author light
 *
 */
public class FCDescriptorToRaster {

	public static void main(String[] args) throws UnknownHostException, IOException {
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap06", 0);
		//加载输入图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("nwroads");
		
		FeatureClassDescriptor pFCDescriptor = new FeatureClassDescriptor();
		pFCDescriptor.create(pFeatureClass, null, "RTE_NUM1");
		
		//设置输出RASTER所需参数，包括路径和cellSize
		RasterWorkspaceFactory pWSF = new RasterWorkspaceFactory();
		IWorkspace pWS = pWSF.openFromFile("C:\\temp", 0);
		RasterConversionOp pConversionOp = new RasterConversionOp();
		pConversionOp.setCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue , 5000);
		
		pConversionOp.toRasterDataset(pFCDescriptor, "GRID", pWS, "rtenumgd");
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
